/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.demister.sgt.cmd;

import java.awt.Graphics;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
 *
 * @author Rock
 */
public class BatchCommand
        extends GraphicsCommand
        implements List<GraphicsCommand> {

    private List<GraphicsCommand> cmds;

    public BatchCommand() {
        cmds = new LinkedList<GraphicsCommand>();
        this.name = "Batch";
    }

    public BatchCommand(Graphics graphics, String name) {
        super(graphics, "Batch");
        cmds = new LinkedList<GraphicsCommand>();
    }

    @Override
    public boolean execute() {
        for (GraphicsCommand cmd : cmds) {
            if (!cmd.execute()) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void undo() {
        for (GraphicsCommand cmd : cmds) {
            cmd.undo();
        }
    }

    @Override
    public String getCode() {
        StringBuffer buffer = new StringBuffer();
        for (GraphicsCommand cmd : cmds) {
            buffer.append(cmd.getCode());
            buffer.append('\n');
        }
        buffer.deleteCharAt(buffer.length() - 1);    //Remove last newline character
        return buffer.toString();
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return cmds.toArray(a);
    }

    @Override
    public Object[] toArray() {
        return cmds.toArray();
    }

    @Override
    public List<GraphicsCommand> subList(int fromIndex, int toIndex) {
        return cmds.subList(fromIndex, toIndex);
    }

    @Override
    public int size() {
        return cmds.size();
    }

    @Override
    public GraphicsCommand set(int index, GraphicsCommand element) {
        return cmds.set(index, element);
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return cmds.retainAll(c);
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return cmds.removeAll(c);
    }

    @Override
    public GraphicsCommand remove(int index) {
        return cmds.remove(index);
    }

    @Override
    public boolean remove(Object o) {
        return cmds.remove(o);
    }

    @Override
    public ListIterator<GraphicsCommand> listIterator(int index) {
        return cmds.listIterator(index);
    }

    @Override
    public ListIterator<GraphicsCommand> listIterator() {
        return cmds.listIterator();
    }

    @Override
    public int lastIndexOf(Object o) {
        return cmds.lastIndexOf(o);
    }

    @Override
    public Iterator<GraphicsCommand> iterator() {
        return cmds.iterator();
    }

    @Override
    public boolean isEmpty() {
        return cmds.isEmpty();
    }

    @Override
    public int indexOf(Object o) {
        return cmds.indexOf(o);
    }

    @Override
    public int hashCode() {
        return cmds.hashCode();
    }

    @Override
    public GraphicsCommand get(int index) {
        return cmds.get(index);
    }

    @Override
    public boolean equals(Object o) {
        return cmds.equals(o);
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return cmds.containsAll(c);
    }

    @Override
    public boolean contains(Object o) {
        return cmds.contains(o);
    }

    @Override
    public void clear() {
        cmds.clear();
    }

    @Override
    public boolean addAll(int index, Collection<? extends GraphicsCommand> c) {
        return cmds.addAll(index, c);
    }

    @Override
    public boolean addAll(Collection<? extends GraphicsCommand> c) {
        return cmds.addAll(c);
    }

    @Override
    public void add(int index, GraphicsCommand element) {
        cmds.add(index, element);
    }

    @Override
    public boolean add(GraphicsCommand e) {
        return cmds.add(e);
    }
}
